<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions({ ageRanges, maleCounts, femaleCounts } = {}) {
      this.chart.setOption({
        title: {
          text: '年龄分布',
          left: 'center',
          textStyle: {
            fontSize: 16,
            fontWeight: 'normal',
            color: '#666'
          }
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          },
          formatter: function(params) {
            let result = `${params[0].name}<br/>`
            let total = 0
            params.forEach(param => {
              result += `${param.marker} ${param.seriesName}: ${param.value}人<br/>`
              total += param.value
            })
            result += `总计: ${total}人`
            return result
          }
        },
        legend: {
          data: ['男性', '女性'],
          bottom: 10,
          textStyle: {
            color: '#666'
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '15%',
          top: '15%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          data: ageRanges || [],
          axisLabel: {
            color: '#999',
            interval: 0
          },
          axisLine: {
            lineStyle: {
              color: '#e6e6e6'
            }
          }
        },
        yAxis: {
          type: 'value',
          name: '人数',
          nameTextStyle: {
            color: '#666'
          },
          axisLabel: {
            color: '#999'
          },
          splitLine: {
            lineStyle: {
              color: '#f5f5f5'
            }
          }
        },
        series: [
          {
            name: '男性',
            type: 'bar',
            data: maleCounts || [],
            itemStyle: {
              color: {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [{
                  offset: 0, color: '#409EFF'
                }, {
                  offset: 1, color: '#79BBFF'
                }]
              }
            },
            emphasis: {
              itemStyle: {
                color: '#337ECC'
              }
            },
            animationDelay: function (idx) {
              return idx * 100;
            }
          },
          {
            name: '女性',
            type: 'bar',
            data: femaleCounts || [],
            itemStyle: {
              color: {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [{
                  offset: 0, color: '#FF6B9D'
                }, {
                  offset: 1, color: '#FF9DC6'
                }]
              }
            },
            emphasis: {
              itemStyle: {
                color: '#E6537A'
              }
            },
            animationDelay: function (idx) {
              return idx * 100 + 50;
            }
          }
        ],
        animationEasing: 'elasticOut',
        animationDelayUpdate: function (idx) {
          return idx * 5;
        }
      })
    }
  }
}
</script>